package cn.ffcs.uec.insurance.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Tab;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.insurance.action.bean.UecBmclassMainBean;
import cn.ffcs.uec.insurance.constant.UecBmConstants;
import cn.ffcs.uec.insurance.model.UecBmclass;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.UomZkUtil;

@Controller
@Scope("prototype")
public class UecBmclassMainComposer extends BasePortletComposer implements
		IPortletInfoProvider {

	private static final long serialVersionUID = -5929030282722775845L;

	private UecBmclassMainBean bean = new UecBmclassMainBean();

	private UecBmclass uecBmclass;
	
	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
		Components.wireVariables(comp, bean);
		bean.getUecBmclassListboxExt().setPortletInfoProvider(this);
		bean.getUecBmclassListboxExt().addForward(
				UecBmConstants.ON_UEC_BMCLASS_QUERY, comp,
				UecBmConstants.ON_UEC_CLASSITEM_QUERY_RESPONSE);

		bean.getUecBmclassListboxExt().addForward(
				UecBmConstants.ON_CLEAN_TAB_UEC_BMCLASS_QUERY, comp,
				UecBmConstants.ON_CLEAN_TAB_UEC_BMCLASS_QUERY_RESPONSE);
		
	}

	/**
	 * 界面初始化
	 */
	public void onCreate$uecBmclassMainWin() {

	}

	/**
	 * 查询到外包员工基本信息的响应处理.
	 * 
	 * @param event
	 */
	public void onUecClassitemQueryResponse(final ForwardEvent event) {
		uecBmclass = (UecBmclass) event.getOrigin().getData();
		if(uecBmclass != null){
			callTab();
		}
	}

	public void onClickTab(ForwardEvent event) {
		try {
			Event origin = event.getOrigin();
			if (origin != null) {
				Component comp = origin.getTarget();
				if (comp != null && comp instanceof Tab) {
					bean.setTab((Tab) comp);
					callTab();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * tab响应
	 * 
	 * @throws Exception
	 * 
	 */
	public void callTab() {
		try {
			if (bean.getTab() == null) {
				bean.setTab(bean.getTabBox().getSelectedTab());
			}
			if (uecBmclass != null) {
				String tab = this.bean.getTab().getId();
				if ("classitemTab".equals(tab)) {
					Events.postEvent(UecBmConstants.ON_UEC_CLASSITEM_QUERY,
							this.bean.getUecClassitemListboxExt(), uecBmclass);
				} else if ("periodstateTab".equals(tab)) {
					Events.postEvent(UecBmConstants.ON_UEC_PERIODSTATE_QUERY,
							this.bean.getUecPeriodstateListboxExt(), uecBmclass);
				}
			} else {
				// 清空数据
				bean.getUecClassitemListboxExt().onCleanUecClassitemResponse(null);
				bean.getUecPeriodstateListboxExt().onCleanUecPeriodstateResponse(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 处理点击后Tabs .
	 * 
	 * @param event
	 */
	public void onCleanTabUecBmclassQueryResponse(final ForwardEvent event) {
		uecBmclass = (UecBmclass) event.getOrigin().getData();
		if(uecBmclass == null){
			Events.postEvent(UecBmConstants.ON_CLEAN_UEC_CLASSITEM,
					bean.getUecClassitemListboxExt(), null);
			Events.postEvent(UecBmConstants.ON_CLEAN_UEC_PERIODSTATE,
					bean.getUecPeriodstateListboxExt(), null);
			callTab();
		}
	}

}
